package com.fingard.dsp.bank.directbank.ccb02;


import java.util.HashMap;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CCB02SinglePay extends CCB02Base implements ISinglePay {

    //单笔支付，不需采用流处理方式
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        tmpRetDetail = d_190802(p_req1908);

        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    //190802对外支付
    private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        //赋值通用参数
        CCB02Header bankHeader = new CCB02Header();
        bankHeader.REQUEST_SN = codeToAscii(p_req1908.singleDetail.askNumber);
        if (p_req1908.singleDetail.sameBnk.equals("1")) {
            bankHeader.TX_CODE = "6W8010";//6W8010 自动转帐交易 行内对公
        }
        //else if(StringHelper.hasAnyChar(p_req1908.singleDetail.cnapsCode)){
        //	bankHeader.TX_CODE = "6W8060";//6W8060 跨行实时转账
        //}
        else {
            bankHeader.TX_CODE = "6W8020";//跨行公对公
        }

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;//报文串需要整个字符串传送，中间需无回车符，否则会签名出错
        writeCommonHeader(xmlWriter, bankHeader);
        //需要进行对账码拼接以及节点解析，采用XElement进行处理
        XElement tmpXElement = new XElement("TX_INFO", "");
        if (bankHeader.TX_CODE.equals("6W8060")) {
            tmpXElement.addChild(new XElement("TRAN_TYPE", "0"));//0-建行转他行,预留标志
        }
        tmpXElement.addChild(new XElement("PAY_ACCNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("RECV_ACCNO", p_req1908.singleDetail.recAct));
        String oppBL = p_req1908.singleDetail.getOppBankLocation();
        if (p_req1908.singleDetail.sameBnk.equals("1")) {

            CCB02OtherElse ccbOther = new CCB02OtherElse();
            ccbOther.setSbLog(this.getSbLog());
            ccbOther.setLogFlushTime(this.logFlushTime);
            ccbOther.reqHeader = this.reqHeader;
            ccbOther.bankFront = this.bankFront;
            HashMap<String, String> tmpHash = ccbOther.exec6W0101(p_req1908);
            if (tmpHash.containsKey("UBANK_NO")) {
                tmpXElement.addChild(new XElement("RECV_UBANKNO", tmpHash.get("UBANK_NO")));
            } else {
                tmpXElement.addChild(new XElement("RECV_UBANKNO", ""));
            }

            tmpXElement.addChild(new XElement("RECV_ACC_NAME", p_req1908.singleDetail.recName));
            tmpXElement.addChild(new XElement("CHK_RECVNAME", "1"));
            if (StringHelper.isNullOrEmpty(oppBL)) {
                oppBL = "中国建设银行";
            }
            tmpXElement.addChild(new XElement("RECV_OPENACC_DEPT", oppBL));
            tmpXElement.addChild(new XElement("RECV_EXCHANGENO", "00000"));
            if (tmpHash.containsKey("COUNTER_NO")) {
                tmpXElement.addChild(new XElement("RECV_COUNTERNO", tmpHash.get("COUNTER_NO")));
            } else {
                tmpXElement.addChild(new XElement("RECV_COUNTERNO", ""));
            }
        } else {
            tmpXElement.addChild(new XElement("RECV_ACC_NAME", p_req1908.singleDetail.recName));
            if (bankHeader.TX_CODE.equals("6W8060")) {
                tmpXElement.addChild(new XElement("RECV_UBANKNO", p_req1908.singleDetail.cnapsCode));
                tmpXElement.addChild(new XElement("RECV_OPENACC_DEPT", oppBL));
            } else {
                tmpXElement.addChild(new XElement("RECV_OPENACC_DEPT", oppBL));
                tmpXElement.addChild(new XElement("RECV_UBANKNO", p_req1908.singleDetail.cnapsCode));
            }
        }

        tmpXElement.addChild(new XElement("AMOUNT", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("CUR_TYPE", convertCurToBank(p_req1908.singleDetail.payCur)));
        tmpXElement.addChild(new XElement("USEOF", p_req1908.singleDetail.purpose));
        if (!bankHeader.TX_CODE.equals("6W8060")) {
            tmpXElement.addChild(new XElement("REM1", p_req1908.singleDetail.abstractATS));
            tmpXElement.addChild(new XElement("REM2", p_req1908.singleDetail.askNumber));
        }

        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());

        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeElementClosed("SIGN_INFO");
        xmlWriter.writeElementClosed("SIGNCERT");
        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String RETURN_CODE = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                String RETURN_MSG = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                //String DEAL_TYPE = tmpBkReader.getFirstText("/TX/TX_INFO/DEAL_TYPE");

                retDetail.respCode = "0";
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;//tmpBkReader.getFirstText("/TX/REQUEST_SN");

                if (bankHeader.TX_CODE.equals("6W8010")) {
                    retDetail.transState = bankHeader.TX_CODE + "+" + RETURN_CODE;// + "+" + DEAL_TYPE;
                } else {
                    retDetail.transState = RETURN_CODE;// + "+" + DEAL_TYPE;
                }
                retDetail.payInfoCode = RETURN_CODE;
                retDetail.respInfo = RETURN_MSG;
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //1944 状态查询
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);

        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        CCB02Header bankHeader = new CCB02Header();
        bankHeader.REQUEST_SN = p_req1944.reqHeader.transSeqID;
        bankHeader.TX_CODE = "6W0600";

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
        xmlWriter.writeStartElement("TX_INFO");
        xmlWriter.writeElementString("REQUEST_SN1", codeToAscii(p_req1944.askNumber));
        xmlWriter.writeEndDocument();
        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String RETURN_CODE = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    String RETURN_MSG = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                    if (RETURN_CODE.equals("000000")) {
                        if (tmpBkReader.getFirstText("/TX/REQUEST_SN").equals(p_req1944.reqHeader.transSeqID)) {
                            tmpRetAtsDetail.respCode = "0";
                            tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                        } else {
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "请求REQUEST_SN与响应不相符";
                        }
                        tmpRetAtsDetail.respInfo = tmpBkReader.getFirstText("/TX/TX_INFO/MESSAGE");
                        tmpRetAtsDetail.payInfoCode = tmpBkReader.getFirstText("/TX/TX_INFO/MESSAGE");
                        tmpRetAtsDetail.transState = bankHeader.TX_CODE + "+" + tmpBkReader.getFirstText("/TX/TX_INFO/DEAL_RESULT");

                    } else if (RETURN_CODE.equals("0130Z110BF34")) {
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                        tmpRetAtsDetail.transState = bankHeader.TX_CODE + "+" + RETURN_CODE;
                        tmpRetAtsDetail.payInfoCode = RETURN_CODE;
                        String MESSAGE = tmpBkReader.getFirstText("/TX/TX_INFO/MESSAGE");
                        if (StringHelper.hasAnyChar(MESSAGE)) {
                            tmpRetAtsDetail.respInfo = MESSAGE;
                        } else {
                            tmpRetAtsDetail.respInfo = RETURN_MSG;
                        }
                    } else {
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = RETURN_MSG;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
